﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EGym.BL;
using EGym.BL.DataGestor;
using EGym.BL.Forms;
using EGym.Data;

namespace EGym.Controllers
{
    public partial class AfiliadoController : BaseController
    {
        #region Constantes
        private const int AFILIADO_CODIGO_CADASTRO_PAI = 1;
        #endregion

        //
        // GET: /Afiliado/

        public ActionResult Index()
        {
            return MeInscrever();
        }
        
        //
        // GET: /Afiliado/MeInscrever
        public ActionResult MeInscrever()
        {
            AfiliadoGestor gestor = new AfiliadoGestor(new DBDataContext());
            AfiliadoForm form = ControllerHelper.GetFromSession("Form") != null? (AfiliadoForm)ControllerHelper.GetFromSession("Form") : gestor.GetFormForInsert();

            ViewData["TipoDePessoa"] = form.TipoDePessoa;

            return View(form);
        }
        
        //
        // GET: /Afiliado/Perfil
        public ActionResult MeuPerfil()
        {
            if (SessionManager.EstaAutenticado())
            {
                AfiliadoGestor gestor = new AfiliadoGestor(new DBDataContext());


                AfiliadoForm form = gestor.GetFormForEdit(SessionManager.Usuario.AfiliadoId);

                ViewData["TipoDePessoa"] = form.TipoDePessoa;
                ViewData["TipoDeEndereco"] = form.TipoDeEndereco;
                ViewData["Estado"] = form.Estado;

                return View(form);
            }
            return RedirectToAction("SessaoExpirada", "Home");
        }
        
        //
        // Post: /Afiliado/MeInscrever
        [HttpPost]
        public ActionResult MeInscrever(FormCollection formCollection)
        {
            AfiliadoGestor gestor = new AfiliadoGestor(new DBDataContext());
            AfiliadoForm form = gestor.GetFormForInsert();

            UpdateModel(form, formCollection);

            gestor.EfeutaInscricao(form);

            if (form.DadosSaoValidos())
                return RedirectToAction("MeuPerfil");

            ControllerHelper.AddToSession("Form",form);

            return RedirectToAction("MeInscrever");
        }

        [HttpPost]
        public ActionResult AssociaEndereco(FormCollection formCollection)
        {
            AfiliadoForm form = null;
            int afiliadoId = Convert.ToInt32(formCollection["Afiliado.AfiliadoId"]);

            if (afiliadoId > 0 )
            {
                DBDataContext db = new DBDataContext();
                AfiliadoGestor afiliadoGestor = new AfiliadoGestor(db);
                EnderecoGestor enderecoGestor = new EnderecoGestor(db);
                form = afiliadoGestor.GetFormForEdit(afiliadoId);

                UpdateModel(form, formCollection);

                form.EnderecoDoAfiliado.EnderecoTipo = enderecoGestor.GetTipoDeEnderecoByNome(form.TipoDeEnderecoSelecionada);
                form.EnderecoDoAfiliado.Estado = enderecoGestor.GetEstadoByNome(form.EstadoSelecionado);

                enderecoGestor.AdcionarEnderecoAssociadoComAfiliacaoAssociada(afiliadoId, form.EnderecoDoAfiliado);
            }

            return RedirectToAction("MeuPerfil");
        }
    }
}
